package com.lx.mvc;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @ClassName: HandlerMapping
 * @Description: 映射器（包含了网址与方法的对应关系）
 * @Date Created in  2021-07-03 11:35
 * @Modified By:
 * @Author: 零巽
 */
public class HandlerMapping {
    private static Map<String, MACMapping> data = new HashMap<>();

    public static MACMapping get(String uri) {
        return data.get(uri);
    }

    public static void load(InputStream inputStream) {
        Properties properties = new Properties();
        try {
            properties.load(inputStream);//加载配置文件
        } catch (IOException e) {
            e.printStackTrace();
        }
        Collection<Object> values = properties.values();//获取配置文件中的所有键值对中的值（类）
        for (Object cla : values) {
            String className = (String) cla;
            try {
                Class c = Class.forName(className);//通过类名加载配置文件中的类
                Object o = c.getConstructor().newInstance();//调用类的构造方法创建类对象
                //获取类中的方法
                Method[] methods = c.getMethods();
                for (Method method : methods) {
                    //获取方法的注解
                    Annotation[] annotations = method.getAnnotations();
                    if (annotations != null) {
                        for (Annotation annotation : annotations) {
                            if (annotation instanceof ResponseBody) {
                                //说明此方法用于返回字符串给客户端
                                MACMapping macMapping = new MACMapping(o,method,ResponseType.TEXT);
                                Object obj = data.put(((ResponseBody) annotation).value(), macMapping);
                                if (obj != null) {
                                    throw new RuntimeException("请求地址重复"+((ResponseBody) annotation).value());
                                }
                            } else if (annotation instanceof ResponseView) {
                                //说明此方法用于返回界面给客户端
                                MACMapping macMapping = new MACMapping(o,method,ResponseType.VIEW);
                                Object obj = data.put(((ResponseView) annotation).value(), macMapping);
                                if (obj != null) {
                                    throw new RuntimeException("请求地址重复"+((ResponseView) annotation).value());
                                }
                            }
                        }

                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 映射对象，每一个对象封装了一个方法，用于处理请求
     */
    public static class MACMapping {
        private Object object;
        private Method method;
        private ResponseType type;

        public MACMapping(Object object, Method method, ResponseType type) {
            this.object = object;
            this.method = method;
            this.type = type;
        }

        public MACMapping() {
        }

        public Object getObject() {
            return object;
        }

        public void setObject(Object object) {
            this.object = object;
        }

        public Method getMethod() {
            return method;
        }

        public void setMethod(Method method) {
            this.method = method;
        }

        public ResponseType getType() {
            return type;
        }

        public void setType(ResponseType type) {
            this.type = type;
        }
    }

}
